Find Sum of Natural Numbers

Theory:

A natural number is a whole number greater than 0. The sum of natural numbers up to a given limit can be calculated using the formula: sum = (limit * (limit + 1)) / 2.

Python Code:

def sum_of_natural_numbers(limit):
    return (limit * (limit + 1)) // 2

# Taking input for the limit and calculating the sum of natural numbers
def calculate_and_display_sum():
    limit = int(input("Enter the limit: "))
    sum_natural_numbers = sum_of_natural_numbers(limit)
    print("Sum of natural numbers up to", limit, "is:", sum_natural_numbers)

calculate_and_display_sum()

Example Output 1:

Enter the limit: 5

Sum of natural numbers up to 5 is: 15

Example Output 2:

Enter the limit: 10

Sum of natural numbers up to 10 is: 55

Code Explanation:

The function sum_of_natural_numbers(limit) calculates the sum of natural numbers up to a given limit using the formula.

The function calculate_and_display_sum() takes input for the limit, calculates the sum using the aforementioned function, and displays the result.